﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using crm_library.DataAccess.ObjectReps;
using crm_library.DataAccess;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class SaleItem : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "SaleItem";
		public new const string ObjectName = "SaleItem";

		public SaleItem(int saleItemID) : base(TableName, ObjectName)
		{
			SaleItemID = saleItemID;
			Load();
		}

		public SaleItem()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("Sale/Item ID")]
		[DataFieldAttribute("ItemID", false, true)]
		[DataMember]
		public int SaleItemID { get; set; }

		[DataFieldAttribute("ProductID", false)]
		[DataMember]
		public int ProductID { get; set; }

		private Product _productObj;
		public Product ProductObj
		{
			get
			{
				if (_productObj == null)
				{
					_productObj = new Product(ProductID);
				}
				return _productObj;
			}
		}

		[ListingFieldAttribute("Product")]
		public string ProductName
		{
			get
			{
				return ProductObj.ProductName;
			}
		}


		[DataFieldAttribute("SaleID", false)]
		[DataMember]
		public int SaleID { get; set; }

		private Sale _saleObj;
		public Sale SaleObj
		{
			get
			{
				if (_saleObj == null)
				{
					_saleObj = new Sale(SaleID);
				}
				return _saleObj;
			}
		}

		[ListingFieldAttribute("Qty")]
		[DataFieldAttribute("Quantity", false)]
		[DataMember]
		public int Quantity { get; set; }

		[ListingFieldAttribute("Price")]
		[DataFieldAttribute("Price", false)]
		[DataMember]
		public decimal Price { get; set; }

		[ListingFieldAttribute("Total")]
		public decimal Total { get { return Quantity * Price; } }


		#endregion properties

		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return SaleItemID;
		}

		public override void SetPKValue(int pk)
		{
			SaleItemID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new SaleItem()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			DB.ReadObject(this, pk);
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			const string format = "{0}x{1}";
			return new ListItem(String.Format(format, Quantity, ProductName), GetPKValue().ToString());
		}

		#endregion ObjectRepresentation implementations

		public static List<SaleItem> GetAll()
		{
			return DB.ReadObjectList(new SaleItem());
		}
	}
}
